<?php
namespace app\index\controller;
use think\Config;
use think\Controller;
use think\Cookie;
use think\Request;
use think\Session;
class  Base extends Controller {

    public $uid;
    public $user;
    public $system_shop;



    function __construct(Request $request = null)
    {
        parent::__construct($request);
//        header("Content-type:text/html;charset=utf-8");

        //接收推荐码
        if(isset($_GET['tjr'])){
            $tjr = trim($_GET['tjr']) ;
            if($tjr){
                $tj_user = model('user')->where('uid',$tjr)->find();
                if($tj_user){
                    Session::set('puid',$tjr);
                    Session::set('ppuid',$tj_user->ppuid);
                }
            }
        }
        //检测登录
        $this->uid = Session::get('uid')?Session::get('uid'):Cookie::get('uid');
        $this->uid = 1;
        if(!$this->uid){
            $url = Request::instance()->url(true);
            Session::set('fromUrl',$url);
            $this->redirect('login/login');

        }
        $user = model('user')->where('uid',$this->uid)->find();
        $this->user = $user;
        $this->assign('name',$user->nickname);

        $v = 10; // 版本号
        $this->assign('uid',$this->uid);
        $this->assign('v',$v);
        $this->assign('order_status', Config::get('order_status'));
        $this->assign('order_status', Config::get('order_status'));
        $this->assign('min_profit', getSettingValue('min_profit')?getSettingValue('min_profit'):20);

    }



    public function getMyShop(){

        $shop = getUserShop($this->uid);
        if(!$shop){
            $user = $this->user;
            $num = getSettingValue('open_shop_num');
            if($user->grade>=$num){
                //去创建店铺
                $this->redirect('shop/myShopSet');
            }
            else{
                $this->error('升级'.$num.'星会员才能创建店铺，快去升级',url('index/promotion/index'));
            }
        }
        return $shop;
    }


    public function isCanAddProduct(){
        $user = $this->user;
        $num = getSettingValue('add_product_num');
        if($user->grade<$num){
            $this->error('升级'.$num.'星会员才能上传自己的商品，快去升级',url('index/promotion/index'));
        }


    }




}